package com.yanfan.attach.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yanfan.attach.domain.DeviceDetail;
import com.yanfan.attach.mapper.DeviceDetailMapper;
import com.yanfan.attach.service.IDeviceDetailService;
import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.common.utils.bean.BeanUtils;
import com.yanfan.iot.domain.Device;
import com.yanfan.iot.service.IDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class DeviceDetailServiceImpl extends ServiceImpl<DeviceDetailMapper, DeviceDetail> implements IDeviceDetailService {
    @Autowired
    private DeviceDetailMapper deviceDetailMapper;
    @Autowired
    private IDeviceService deviceService;


    @Override
    public DeviceDetail insertDeviceDetail(DeviceDetail deviceDetail) {
        Device device = deviceService.insertDevice(deviceDetail);
        deviceDetailMapper.insertSelective(deviceDetail);
        BeanUtils.copyProperties(device, deviceDetail);
        return deviceDetail;
    }

    @Override
    public AjaxResult updateDeviceDetail(DeviceDetail deviceDetail) {
        DeviceDetail dbDetail = deviceDetailMapper.selectDeviceByDeviceId(deviceDetail.getDeviceId());
        if (Objects.isNull(dbDetail) || Objects.isNull(dbDetail.getDetailId())) {
            deviceDetailMapper.insertSelective(deviceDetail);
        } else {
            deviceDetailMapper.updateByDeviceIdSelective(deviceDetail);
        }
        return deviceService.updateDevice(deviceDetail);
    }

    @Override
    public List<DeviceDetail> selectAllDeviceShortList(DeviceDetail device) {
        return deviceDetailMapper.selectDeviceDetailList(device);
    }
}
